{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import shutil\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "ordered_path = r'C:\\Users\\luke_\\Desktop\\[Projects]NaoDao_ClosedBeta_1st\\NaoDao_DATA\\OnlineDATA(Pavlovia)\\OrderedDATA'\n",
    "cleaned_path = r'C:\\Users\\luke_\\Desktop\\[Projects]NaoDao_ClosedBeta_1st\\NaoDao_DATA\\OnlineDATA(Pavlovia)\\CleanedDATA'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Define `get_useful_cols` functions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1 IAT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# define functions\n",
    "def get_IAT_useful_cols(data):\n",
    "    useful_iat_cols = ['response.interval_time',\n",
    "                       'block_order_type',\n",
    "                       'response.corr',\n",
    "                       'response.rt',\n",
    "                       'BlockType',\n",
    "                       'TrialType',\n",
    "                       '请填写您的姓名：',\n",
    "                       '请填写您的学号：']\n",
    "    data = data[useful_iat_cols]\n",
    "    return data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2 Mental Rotation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Mental Rotation\n",
    "def get_Mental_useful_cols(data):\n",
    "    useful_mentalrotation_cols = ['response.keys',\n",
    "                                  'response.corr',\n",
    "                                  'response.rt',\n",
    "                                  'pic_orientation',\n",
    "                                  'trial_type',\n",
    "                                  '请填写您的姓名：',\n",
    "                                  '请填写您的学号：']\n",
    "    data = data[useful_mentalrotation_cols]\n",
    "    return data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.3 Stroop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Stroop\n",
    "# 实验数据里没有区分practice block和test block\n",
    "# 前24行记录是练习数据，后期处理做一下区分\n",
    "def get_stroop_useful_cols(data):\n",
    "    useful_stroop_cols = ['exp_resp.corr',\n",
    "                          'exp_resp.rt',\n",
    "                          'trial_type',\n",
    "                          '请填写您的姓名：',\n",
    "                          '请填写您的学号：']\n",
    "    data = data[useful_stroop_cols]\n",
    "    return data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.4 MID"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# MID\n",
    "def get_MID_useful_cols(data):\n",
    "    useful_MID_cols = ['trial_type',\n",
    "                       'hit',\n",
    "                       'not_hit',\n",
    "                       'block_type',\n",
    "                       'corr_label',\n",
    "                       'response.rt',\n",
    "                       'actual_rt',\n",
    "                       'block_money',\n",
    "                       '请填写您的姓名：',\n",
    "                       '请填写您的学号：']\n",
    "    data = data[useful_MID_cols]\n",
    "    return data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.5 Multi-tasking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Multi-tasking\n",
    "def get_multi_useful_cols(data):\n",
    "    useful_multi_cols = ['this_block',\n",
    "                         'response.keys',\n",
    "                         'response.corr',\n",
    "                         'response.rt',\n",
    "                         'trial_type',\n",
    "                         'block_type',\n",
    "                         'stim_pic',\n",
    "                         '请填写您的姓名：',\n",
    "                         '请填写您的学号：']\n",
    "    data = data[useful_multi_cols]\n",
    "    return data\n",
    "\n",
    "def get_congruency_cols(data):\n",
    "    rows = data.shape[0]\n",
    "    data['congruency'] = ''\n",
    "    for row in range(0,rows):\n",
    "        if 'diamond_dot2' in data['stim_pic'].iloc[row]:\n",
    "            data['congruency'].iloc[row] = 'congruent'\n",
    "        elif 'diamond_dot3' in data['stim_pic'].iloc[row]:\n",
    "            data['congruency'].iloc[row] = 'incongruent'\n",
    "        elif 'square_dot2' in data['stim_pic'].iloc[row]:\n",
    "            data['congruency'].iloc[row] = 'incongruent'\n",
    "        else:\n",
    "            data['congruency'].iloc[row] = 'congruent'\n",
    "    return data\n",
    "\n",
    "def get_trialtype_cols(data):\n",
    "    rows = data.shape[0] - 1\n",
    "    data['trial_type'] = ''\n",
    "    for row in range(0,rows):\n",
    "        tasktype1 = data['task_type'].iloc[row]\n",
    "        tasktype2 = data['task_type'].iloc[row + 1]\n",
    "        if tasktype2 == tasktype1:\n",
    "            data['trial_type'].iloc[row + 1] = 'repeated'\n",
    "        else:\n",
    "            data['trial_type'].iloc[row + 1] = 'switch'\n",
    "    return data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Clean Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "order_lists = ['Order1','Order2']\n",
    "task_lists = ['IAT', 'MentalRotation', 'MID', 'Multi-tasking', 'Stroop']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1 IAT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(0,len(order_lists)):\n",
    "    task_path = f'C:/Users/luke_/Desktop/[Projects]NaoDao_ClosedBeta_1st/NaoDao_DATA/OnlineDATA(Pavlovia)/OrderedDATA/{order_lists[i]}/IAT'\n",
    "    os.chdir(task_path)\n",
    "    filelists = os.listdir()\n",
    "    move_path = f'C:/Users/luke_/Desktop/[Projects]NaoDao_ClosedBeta_1st/NaoDao_DATA/OnlineDATA(Pavlovia)/CleanedDATA/{order_lists[i]}/IAT'\n",
    "    os.mkdir(move_path)\n",
    "    dummy_number = 0\n",
    "    for file in filelists:\n",
    "        # 读取文件\n",
    "        data = pd.read_csv(file,engine='python',sep=',')\n",
    "        # 选中需要进行分析的列\n",
    "        data = get_IAT_useful_cols(data)\n",
    "        # 删掉空行\n",
    "        data = data.dropna(how='all')\n",
    "        # output\n",
    "        dummy_number += 1\n",
    "        new_name = str(dummy_number) + '_' + 'cleaned' + '.xlsx' \n",
    "        data.to_excel(new_name)\n",
    "        # 移动到cleaned data文件夹\n",
    "        shutil.move(new_name,move_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.2 Mental Rotation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(0,len(order_lists)):\n",
    "    task_path = f'C:/Users/luke_/Desktop/[Projects]NaoDao_ClosedBeta_1st/NaoDao_DATA/OnlineDATA(Pavlovia)/OrderedDATA/{order_lists[i]}/MentalRotation'\n",
    "    os.chdir(task_path)\n",
    "    filelists = os.listdir()\n",
    "    move_path = f'C:/Users/luke_/Desktop/[Projects]NaoDao_ClosedBeta_1st/NaoDao_DATA/OnlineDATA(Pavlovia)/CleanedDATA/{order_lists[i]}/MentalRotation'\n",
    "    os.mkdir(move_path)\n",
    "    dummy_number = 0\n",
    "    for file in filelists:\n",
    "        # 读取文件\n",
    "        data = pd.read_csv(file,engine='python',sep=',')\n",
    "        # 选中需要进行分析的列\n",
    "        data = get_Mental_useful_cols(data)\n",
    "        # 删掉空行\n",
    "        data = data.dropna(how='any')\n",
    "        # output\n",
    "        dummy_number += 1\n",
    "        new_name = str(dummy_number) + '_' + 'cleaned' + '.xlsx' \n",
    "        data.to_excel(new_name)\n",
    "        # 移动到cleaned data文件夹\n",
    "        shutil.move(new_name,move_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.3 Stroop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(0,len(order_lists)):\n",
    "    task_path = f'C:/Users/luke_/Desktop/[Projects]NaoDao_ClosedBeta_1st/NaoDao_DATA/OnlineDATA(Pavlovia)/OrderedDATA/{order_lists[i]}/Stroop'\n",
    "    os.chdir(task_path)\n",
    "    filelists = os.listdir()\n",
    "    move_path = f'C:/Users/luke_/Desktop/[Projects]NaoDao_ClosedBeta_1st/NaoDao_DATA/OnlineDATA(Pavlovia)/CleanedDATA/{order_lists[i]}/Stroop'\n",
    "    os.mkdir(move_path)\n",
    "    dummy_number = 0\n",
    "    for file in filelists:\n",
    "        # 读取文件\n",
    "        data = pd.read_csv(file,engine='python',sep=',')\n",
    "        # 选中需要进行分析的列\n",
    "        data = get_stroop_useful_cols(data)\n",
    "        # 删掉空行\n",
    "        data = data.dropna(how='any')\n",
    "        # output\n",
    "        dummy_number += 1\n",
    "        new_name = str(dummy_number) + '_' + 'cleaned' + '.xlsx' \n",
    "        data.to_excel(new_name)\n",
    "        # 移动到cleaned data文件夹\n",
    "        shutil.move(new_name,move_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.4 MID"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(0,len(order_lists)):\n",
    "    task_path = f'C:/Users/luke_/Desktop/[Projects]NaoDao_ClosedBeta_1st/NaoDao_DATA/OnlineDATA(Pavlovia)/OrderedDATA/{order_lists[i]}/MID'\n",
    "    os.chdir(task_path)\n",
    "    filelists = os.listdir()\n",
    "    move_path = f'C:/Users/luke_/Desktop/[Projects]NaoDao_ClosedBeta_1st/NaoDao_DATA/OnlineDATA(Pavlovia)/CleanedDATA/{order_lists[i]}/MID'\n",
    "    os.mkdir(move_path)\n",
    "    dummy_number = 0\n",
    "    for file in filelists:\n",
    "        # 读取文件\n",
    "        data = pd.read_csv(file,engine='python',sep=',')\n",
    "        # 选中需要进行分析的列\n",
    "        data = get_MID_useful_cols(data)\n",
    "        # 删掉空行\n",
    "        data = data.dropna(how='all')\n",
    "        # output\n",
    "        dummy_number += 1\n",
    "        new_name = str(dummy_number) + '_' + 'cleaned' + '.xlsx' \n",
    "        data.to_excel(new_name)\n",
    "        # 移动到cleaned data文件夹\n",
    "        shutil.move(new_name,move_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.5 Multi-tasking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(0,len(order_lists)):\n",
    "    task_path = f'C:/Users/luke_/Desktop/[Projects]NaoDao_ClosedBeta_1st/NaoDao_DATA/OnlineDATA(Pavlovia)/OrderedDATA/{order_lists[i]}/Multi-tasking'\n",
    "    os.chdir(task_path)\n",
    "    filelists = os.listdir()\n",
    "    move_path = f'C:/Users/luke_/Desktop/[Projects]NaoDao_ClosedBeta_1st/NaoDao_DATA/OnlineDATA(Pavlovia)/CleanedDATA/{order_lists[i]}/Multi-tasking'\n",
    "    os.mkdir(move_path)\n",
    "    dummy_number = 0\n",
    "    for file in filelists:\n",
    "        # 读取文件\n",
    "        data = pd.read_csv(file,engine='python',sep=',')\n",
    "        # 选中需要进行分析的列\n",
    "        data = get_multi_useful_cols(data)\n",
    "        # 删掉this_block=0的空行\n",
    "        data['this_block'] = data['this_block'].fillna('9999')\n",
    "        index_list = data[data['this_block']=='9999'].index\n",
    "        data = data.drop(index_list)\n",
    "        # 修改列名\n",
    "        data.rename(columns={'trial_type':'task_type'},inplace=True)\n",
    "        # 增加congruency列\n",
    "        get_congruency_cols(data)\n",
    "        # 增加trial_type列\n",
    "        get_trialtype_cols(data)\n",
    "        # 删除stim_pic列\n",
    "        data = data.drop('stim_pic',axis=1)\n",
    "        # 删掉空行\n",
    "        data = data.dropna(how='all')\n",
    "        # 只保留exp_trial\n",
    "        data = data[data['this_block']=='experiment']\n",
    "        # output\n",
    "        dummy_number += 1\n",
    "        new_name = str(dummy_number) + '_' + 'cleaned' + '.xlsx' \n",
    "        data.to_excel(new_name)\n",
    "        # 移动到cleaned data文件夹\n",
    "        shutil.move(new_name,move_path)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "07efdcd4b820c98a756949507a4d29d7862823915ec7477944641bea022f4f62"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
